package app.server.project.main.service.db.dao.cfg;

import app.server.project.core.constants.Def;
import app.server.project.main.service.db.entity.cfg.RechargeCfgEntity;
import app.server.project.main.service.db.mapper.cfg.RechargeCfgMapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.PostConstruct;
import lombok.Data;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Administrator
 * @description 针对表【user_info(用户信息表)】的数据库操作Service实现
 * @createDate 2025-01-16 15:25:22
 */
@Repository
@Data
public class RechargeCfgDao extends ServiceImpl<RechargeCfgMapper, RechargeCfgEntity> {


    private Map<Integer, List<RechargeCfgEntity>> listMap = new HashMap<>();
    private List<RechargeCfgEntity> list;

    public void flush() {
        this.list = this.list();
        this.listMap = list.stream().collect(Collectors.groupingBy(RechargeCfgEntity::getTypeId));
    }

    public List<RechargeCfgEntity> listOrderByType() {
        return this.list(Wrappers.lambdaQuery(RechargeCfgEntity.class)
                .eq(RechargeCfgEntity::getStoreType, Def.Zero)
                .orderByAsc(RechargeCfgEntity::getTypeId));
    }

    public List<RechargeCfgEntity> getStore() {
        return this.list(Wrappers.lambdaQuery(RechargeCfgEntity.class)
                .ne(RechargeCfgEntity::getStoreType, Def.Zero)
                .orderByAsc(RechargeCfgEntity::getTypeId));
    }


    @PostConstruct
    public void init() {
        this.flush();
    }

    public RechargeCfgEntity getData(int typeId, int consumeNum) {
        var list = this.listMap.get(typeId);
        return list.stream().filter(v -> v.getConsumeNum() == consumeNum).findAny().get();
    }

    public RechargeCfgEntity getDataById(int rechargeId) {
        return list.stream().filter(v -> v.getId() == rechargeId).findAny().get();
    }
}




